package net.godcode.t5c.demoapp.pages;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;


/**
 * The Start page provides a map to the demo pages.
 * 
 * @author Chris Lewis Nov 19, 2007 <chris@thegodcode.net>
 * @version $Id: Start.java 24 2007-11-19 20:24:31Z burningodzilla $
 */
public class Start {
	
	private List<String> pages = new ArrayList<String>();
	private String page;
	
	public Start() {
		loadPages();
	}
	
	public List<String> getPages() {
		return pages;
	}
	
	private void loadPages() {
		//TODO use a service
		String path = "net/godcode/t5c/demoapp/pages";
		ClassLoader cld = Thread.currentThread().getContextClassLoader();
		File pagePath = new File(cld.getResource(path).getPath());
		if(pagePath == null) {
			throw new RuntimeException("Failed to enumerate pages!");
		}
		
		File[] pcFiles = pagePath.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				//TODO assuming class-less pages are implemented, we'll want to
				//look for .tml files instead.
				if(!name.endsWith(".class") || name.equals("Start.class")) {
					return false;
				} else {
					return name.indexOf('$') < 0;
				}
			}
		});
		
		for(File f : pcFiles) {
			pages.add(f.getName().replaceAll(".class", ""));
		}
	}

	/**
	 * @return the page
	 */
	public String getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(String page) {
		this.page = page;
	}
	
}